package com.chushouya.controller.manager.admin;

import com.chushouya.common.annotations.AuthSkip;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.manager.dto.admin.bonustype.*;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageRequest;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.chushouya.manager.dto.admin.bonus.PreviewGrantBonusDTO;
import com.chushouya.manager.service.admin.BonusTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * 红包类型管理
 */
@RestController
@Api(tags = ServiceType.PLATFORM +"红包类型管理")
@RequestMapping(ServiceType.PLATFORM + "/bonusType")
public class BonusTypeController {

    @Resource
    private BonusTypeService bonusTypeService;

    @ApiOperation("红包类型列表")
    @GetMapping("/page")
    public Response<PageResult<BonusTypeListDTO>> selectPage(BonusTypeQuery query) {
        return Response.success(bonusTypeService.selectPage(query));
    }

    @ApiOperation("红包类型详情")
    @GetMapping("/detail")
    public Response<BonusTypeDetailDTO> detail(Long id) {
        return Response.success(bonusTypeService.detail(id));
    }

    @ApiOperation("红包券分页列表")
    @GetMapping("/ticketList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "红包分类id", required = true)
    })
    public Response<PageResult<BonusTypeTicketListDTO>> ticketPage(Long typeId, PageRequest pageRequest) {
        return Response.success(bonusTypeService.ticketPage(typeId, pageRequest));
    }

    @ApiOperation("删除红包券")
    @PostMapping("/removeTickets")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "红包券id,多个用-拼接字符串", required = true)
    })
    public Response<Void> removeTickets(String ids) {
        bonusTypeService.removeTickets(Strings.splitToLongList(ids, "-"));
        return Response.success();
    }

    @ApiOperation("新增红包类型")
    @PostMapping("/save")
    @Log(businessType = BusinessType.INSERT)
    public Response<Void> save(@Valid @RequestBody BonusTypeSaveDTO dto) {
        bonusTypeService.save(dto);
        return Response.success();
    }

    @ApiOperation("红包类型编辑")
    @PostMapping("/update")
    @Log(businessType = BusinessType.UPDATE)
    public Response<Void> update(@Valid @RequestBody BonusTypeUpdateDTO dto) {
        bonusTypeService.update(dto);
        return Response.success();
    }

    @ApiOperation("红包类型移除")
    @PostMapping("/remove")
    @Log(businessType = BusinessType.DELETE)
    public Response<Void> remove(Long typeId) {
        bonusTypeService.remove(typeId);
        return Response.success();
    }

    @ApiOperation("预发放红包")
    @PostMapping("/previewGrantBonus")
    public Response<Void> previewGrantBonus(
            @Valid @RequestBody PreviewGrantBonusDTO dto) {
        bonusTypeService.previewGrantBonus(dto);
        return Response.success();
    }

    @ApiOperation("导出未领取的红包券码")
    @GetMapping("exportNotReceiveTicketNo")
    @AuthSkip
    @ApiImplicitParam(name = "typeId", value = "红包类型", required = true)
    public void exportNotReceiveTicketNo(Long typeId, HttpServletResponse response) throws IOException {
        bonusTypeService.exportNotReceiveTicketNo(response,typeId);
    }

}
